/**
 * 主题管理器
 * 负责管理应用的主题色彩、字体大小、暗黑模式等界面个性化设置
 */
class ThemeManager {
    constructor() {
        this.currentTheme = 'default';
        this.fontSize = 'medium';
        this.darkMode = false;
        this.progressStyle = 'classic';
        
        this.themes = {
            default: {
                name: '经典红',
                primary: '#e74c3c',
                primaryDark: '#c0392b',
                primaryLight: '#ec7063'
            },
            blue: {
                name: '海洋蓝',
                primary: '#3498db',
                primaryDark: '#2980b9',
                primaryLight: '#5dade2'
            },
            green: {
                name: '森林绿',
                primary: '#27ae60',
                primaryDark: '#229954',
                primaryLight: '#58d68d'
            },
            purple: {
                name: '优雅紫',
                primary: '#9b59b6',
                primaryDark: '#8e44ad',
                primaryLight: '#bb8fce'
            },
            orange: {
                name: '活力橙',
                primary: '#f39c12',
                primaryDark: '#e67e22',
                primaryLight: '#f8c471'
            }
        };
        
        this.init();
    }
    
    /**
     * 初始化主题管理器
     */
    init() {
        this.loadSettings();
        this.applyTheme();
        this.applyFontSize();
        this.applyDarkMode();
        this.applyProgressStyle();
    }
    
    /**
     * 设置主题色彩方案
     * @param {string} theme - 主题名称
     */
    setTheme(theme) {
        if (this.themes[theme]) {
            this.currentTheme = theme;
            this.applyTheme();
            this.saveSettings();
        }
    }
    
    /**
     * 设置字体大小
     * @param {string} size - 字体大小 (small, medium, large)
     */
    setFontSize(size) {
        const validSizes = ['small', 'medium', 'large'];
        if (validSizes.includes(size)) {
            this.fontSize = size;
            this.applyFontSize();
            this.saveSettings();
        }
    }
    
    /**
     * 切换暗黑模式
     * @param {boolean} enabled - 是否启用暗黑模式
     */
    setDarkMode(enabled) {
        this.darkMode = enabled;
        this.applyDarkMode();
        this.saveSettings();
    }
    
    /**
     * 设置进度环样式
     * @param {string} style - 进度环样式 (classic, modern, minimal)
     */
    setProgressStyle(style) {
        const validStyles = ['classic', 'modern', 'minimal'];
        if (validStyles.includes(style)) {
            this.progressStyle = style;
            this.applyProgressStyle();
            this.saveSettings();
        }
    }
    
    /**
     * 应用主题色彩
     */
    applyTheme() {
        const body = document.body;
        
        // 移除所有主题类
        Object.keys(this.themes).forEach(theme => {
            body.removeAttribute(`data-theme`);
        });
        
        // 应用当前主题
        if (this.currentTheme !== 'default') {
            body.setAttribute('data-theme', this.currentTheme);
        }
        
        // 更新CSS变量
        const theme = this.themes[this.currentTheme];
        const root = document.documentElement;
        root.style.setProperty('--primary-color', theme.primary);
        root.style.setProperty('--primary-dark', theme.primaryDark);
        root.style.setProperty('--primary-light', theme.primaryLight);
    }
    
    /**
     * 应用字体大小
     */
    applyFontSize() {
        const body = document.body;
        
        // 移除所有字体大小类
        body.classList.remove('font-small', 'font-medium', 'font-large');
        
        // 应用当前字体大小
        body.classList.add(`font-${this.fontSize}`);
    }
    
    /**
     * 应用暗黑模式
     */
    applyDarkMode() {
        const body = document.body;
        
        if (this.darkMode) {
            body.classList.add('dark-mode');
        } else {
            body.classList.remove('dark-mode');
        }
    }
    
    /**
     * 应用进度环样式
     */
    applyProgressStyle() {
        const progressRing = document.querySelector('.progress-ring');
        if (progressRing) {
            // 移除所有进度环样式类
            progressRing.classList.remove('progress-classic', 'progress-modern', 'progress-minimal');
            
            // 应用当前进度环样式
            progressRing.classList.add(`progress-${this.progressStyle}`);
        }
    }
    
    /**
     * 获取当前主题设置
     * @returns {Object} 主题设置对象
     */
    getSettings() {
        return {
            theme: this.currentTheme,
            fontSize: this.fontSize,
            darkMode: this.darkMode,
            progressStyle: this.progressStyle
        };
    }
    
    /**
     * 获取可用主题列表
     * @returns {Object} 主题列表
     */
    getAvailableThemes() {
        return this.themes;
    }
    
    /**
     * 保存设置到本地存储
     */
    saveSettings() {
        const settings = this.getSettings();
        localStorage.setItem('pomodoroThemeSettings', JSON.stringify(settings));
    }
    
    /**
     * 从本地存储加载设置
     */
    loadSettings() {
        try {
            const saved = localStorage.getItem('pomodoroThemeSettings');
            if (saved) {
                const settings = JSON.parse(saved);
                this.currentTheme = settings.theme || 'default';
                this.fontSize = settings.fontSize || 'medium';
                this.darkMode = settings.darkMode || false;
                this.progressStyle = settings.progressStyle || 'classic';
            }
        } catch (error) {
            console.warn('Failed to load theme settings:', error);
            // 使用默认设置
            this.currentTheme = 'default';
            this.fontSize = 'medium';
            this.darkMode = false;
            this.progressStyle = 'classic';
        }
    }
    
    /**
     * 重置所有主题设置为默认值
     */
    resetToDefaults() {
        this.currentTheme = 'default';
        this.fontSize = 'medium';
        this.darkMode = false;
        this.progressStyle = 'classic';
        
        this.applyTheme();
        this.applyFontSize();
        this.applyDarkMode();
        this.applyProgressStyle();
        this.saveSettings();
    }
    
    /**
     * 预览主题（临时应用，不保存）
     * @param {string} theme - 主题名称
     */
    previewTheme(theme) {
        if (this.themes[theme]) {
            const originalTheme = this.currentTheme;
            this.currentTheme = theme;
            this.applyTheme();
            
            // 3秒后恢复原主题
            setTimeout(() => {
                this.currentTheme = originalTheme;
                this.applyTheme();
            }, 3000);
        }
    }
}

// 导出ThemeManager类
if (typeof module !== 'undefined' && module.exports) {
    module.exports = ThemeManager;
}